//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
#include "globals.h"

static void obj_release(struct client *c,s32 keyboard_slot)
{
    c->objs[keyboard_slot].state=CLIENT_OBJ_INVALID;
    registry_gbl_unref(&registry[c->objs[keyboard_slot].keyboard.seat_name]);
}

s8 keyboard_close(struct client *c,s32 keyboard_slot)
{
  s32 n=client_obj_unref(c,keyboard_slot);
  if(!n) c->objs[keyboard_slot].state&=~CLIENT_OBJ_VISIBLE;
  return wl_display_ev_delete_id(c,c->objs[keyboard_slot].id);
}

s8 keyboard_new_visible(struct client *c,u32 keyboard_id,s32 seat_name)
{
  s8 r=LWL_OK;
  s32 free_slot=CLIENT_FIRST_VALID_ID;

  while(1){
    if(free_slot==CLIENT_OBJS_MAX){
      r=LWL_ERR;
      break;
    }
    if(c->objs[free_slot].state&CLIENT_OBJ_INVALID){
      c->objs[free_slot].state=CLIENT_OBJ_KEYBOARD|CLIENT_OBJ_VISIBLE;
      c->objs[free_slot].release=obj_release;
      c->objs[free_slot].id=keyboard_id;
      c->objs[free_slot].n=1;//the wire has a reference on it

      registry_gbl_ref(&registry[seat_name]);
      c->objs[free_slot].keyboard.seat_name=seat_name;
      break;
    }
    ++free_slot;    
  }
  return r;
}
